package com.vincent.rocketmqdemo.config;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.vincent.rocketmqdemo.listener.MqMsgListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@EnableConfigurationProperties(RocketMqProperties.class)
@Configuration
public class RocketMqConfig {

    @Autowired
    private RocketMqProperties rocketMqProperties;

//    @Bean(value = "demoConsumer", initMethod = "start")
    public ConsumerBean getConsumerbean() {

        ConsumerBean bean = new ConsumerBean();
        setProperties(bean);
        Map<Subscription, MessageListener> listenerMap = new HashMap<>();
        Subscription subscription = new Subscription();
        subscription.setTopic("chat_event_dev");
//        subscription.setTopic("T-SNAIL-LIFE-ORDER-CREATE");
        subscription.setExpression("");
        listenerMap.put(subscription, new MqMsgListener());
        bean.setSubscriptionTable(listenerMap);
        return bean;
    }

    @Bean(value = "demoProducer", initMethod = "start")
    public ProducerBean getProducerBean() {
        ProducerBean bean = new ProducerBean();
        setProperties(bean);
        return bean;
    }

    private void setProperties(Object bean) {
        Properties properties = new Properties();
        properties.setProperty("AccessKey", rocketMqProperties.getAccessKey());
        properties.setProperty("SecretKey", rocketMqProperties.getSecretKey());
        properties.setProperty("ConsumerId", rocketMqProperties.getCustomerId());
        properties.setProperty("ProducerId", rocketMqProperties.getProducerId());
        properties.setProperty("GROUP_ID", rocketMqProperties.getProducerId());
        setServerAddr(properties);
        try {
            Field field = bean.getClass().getDeclaredField("properties");
            field.setAccessible(true);
            field.set(bean, properties);
            field.setAccessible(false);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    private void setServerAddr(Properties properties) {

        if (!StringUtils.isEmpty(rocketMqProperties.getNameServerAddr()) && !rocketMqProperties.getNameServerAddr().equals("default")) {
            properties.setProperty("NAMESRV_ADDR", rocketMqProperties.getNameServerAddr());
        }
        if (!StringUtils.isEmpty(rocketMqProperties.getOnsAddr()) && !rocketMqProperties.getOnsAddr().equals("default")) {
            properties.setProperty("ONSAddr", rocketMqProperties.getOnsAddr());
        }

    }
}
